package com.ncloud.system.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ncloud.common.utils.DateUtils;
import com.ncloud.system.entity.UserRole;
import com.ncloud.system.mapper.UserRoleMapper;
import com.ncloud.system.service.UserRoleService;

@Service
public class UserRoleServiceImpl implements UserRoleService {
	@Autowired
	private UserRoleMapper userRoleMapper;
	
	@Override
	public void createUserRole(Long userId, Long roleId) {
		UserRole userRole = new UserRole();
		userRole.setUserId(userId);
		userRole.setRoleId(roleId);
		userRole.setEffDate(new Date());
		userRole.setExpDate(DateUtils.addYears(new Date(), 50));
		userRoleMapper.createUserRole(userRole);
	}

	@Override
	public List<String> getRolesByUserId(Long userId) {
		List<String> roles = userRoleMapper.getRolesbyUserId(userId);
		return roles;
	}

	@Override
	public void createUserRole(Long userId,List<String> roleList) {
		// 处理用户和角色关系
		for (int i = 0; i < roleList.size(); i++) {
			String roleId = roleList.get(i);
			createUserRole(userId,Long.parseLong(roleId));
		}
	}

	@Override
	public void deleteUserRole(Long userId, List<Long> roleList) {
		// 处理用户和角色关系
		for (int i = 0; i < roleList.size(); i++) {
			Long roleId = roleList.get(i);
			deleteUserRole(userId,roleId);
		}
	}

	private void deleteUserRole(Long userId, Long roleId) {
		UserRole userRole = new UserRole();
		userRole.setUserId(userId);
		userRole.setRoleId(roleId);
		userRoleMapper.deleteUserRole(userRole);
	}
	
}
